package com.mingtu.guards.fragment;

import android.view.View;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mingtu.common.Eventbus.MyEventBus;
import com.mingtu.common.adapter.MyFragmentPagerAdapter;
import com.mingtu.common.base.BaseFragment;
import com.mingtu.common.entity.TabEntity;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.common.viewpager.NoScrollViewPager;
import com.mingtu.guards.R;
import com.mingtu.guards.utils.MyConstant;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class BioFindFragment extends BaseFragment implements LazyFragmentPagerAdapter.Laziable {

    @BindView(R.id.common_tab)
    CommonTabLayout commonTab;
    @BindView(R.id.view_pager)
    NoScrollViewPager viewPager;
    @BindView(R.id.iv_filte)
    ImageView ivFilte;


    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"生物资源"};
    private BioFragment bioFragment;
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bio_find;
    }

    @Override
    protected void initView(View view) {

    }

    @Override
    protected void initData() {

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        bioFragment = BioFragment.getInstance();

        mFragments.add(bioFragment);



        viewPager.setOffscreenPageLimit(mFragments.size());
        viewPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), mFragments));
        viewPager.setCurrentItem(0);
        viewPager.addOnPageChangeListener(new myOnPageChangeListener());
        viewPager.setNoScroll(false);

        commonTab.setTextBold(1);//选中的时候加错
        commonTab.setTabData(mTabEntities);
        commonTab.setOnTabSelectListener(new MyOnTabSelectListener());

    }

    @Override
    protected void getData() {

    }

    public BioFragment getBioFragment() {
        return bioFragment;
    }

    @OnClick(R.id.iv_filte)
    public void onViewClicked(View v) {
        switch (v.getId()) {
            case R.id.iv_filte:
                EventBus.getDefault().post(new MyEventBus(MyConstant.FILTE_BIO));
                break;
        }
    }


    //点击tab 监听
    private class MyOnTabSelectListener implements OnTabSelectListener {

        @Override
        public void onTabSelect(int position) {
            viewPager.setCurrentItem(position);
            if (position == 1) {
                //处于发现界面的时候，隐藏筛选
                ivFilte.setVisibility(View.GONE);
                EventBus.getDefault().post(new MyEventBus(MyConstant.CLOSE_FILTE_BIO));
            } else if (position == 0) {
                ivFilte.setVisibility(View.VISIBLE);
                EventBus.getDefault().post(new MyEventBus(MyConstant.SHOW_FILTE_BIO));
            }
        }

        @Override
        public void onTabReselect(int position) {

        }
    }

    private class myOnPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            commonTab.setCurrentTab(position);
            if (position == 1) {
                //处于发现界面的时候，隐藏筛选
                ivFilte.setVisibility(View.GONE);
                EventBus.getDefault().post(new MyEventBus(MyConstant.CLOSE_FILTE_BIO));
            } else if (position == 0) {
                ivFilte.setVisibility(View.VISIBLE);
                EventBus.getDefault().post(new MyEventBus(MyConstant.SHOW_FILTE_BIO));
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }


}